package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.FindApplyAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.bean.LogInfo;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 代理商界面
 */
public class FindApplyActivity extends BaseActivity implements HttpCallback, RefreshCallback {
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    EmptyWrapper mAdapter;
    private List<LogInfo> mList = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.activity_findapply;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.colorBackground);
        setToolBar("代理");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new EmptyWrapper(new FindApplyAdapter(mContext, mList));
        mAdapter.setEmptyView(R.layout.view_empty);
        setHeaderAndFooter(R.id.find_apply_SpringView, true, true, this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest
                        (HttpURL.CASH_LOG, mContext, new String[]{"accessid", "member_relation", "cur_page", "page_size"}
                                , new Object[]{SPUtils.getAccessid(mContext), "2", page, count})
                , HttpURL.CASH_LOG, CommonValue.REQUEST_CODE1, this);
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest
                        (HttpURL.AGENT_OVERVIEW, mContext, new String[]{"accessid"}
                                , new Object[]{SPUtils.getAccessid(mContext)})
                , HttpURL.AGENT_OVERVIEW, CommonValue.REQUEST_CODE2, this);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                springView.onFinishFreshAndLoad();
                if (page == 1) {
                    mList.clear();
                }
                canLoadmore = JsonUtils.getInstance().havePage(content);
                List<LogInfo> newmList = JsonUtils.getInstance().getList(content, "log_list", LogInfo.class);
                mList.addAll(newmList);
                mAdapter.notifyDataSetChanged();
                break;
            case CommonValue.REQUEST_CODE2:
                tvNum.setText("代理用户:" + JsonUtils.getInstance().getJsonObject(content, "direct_users") + "个");
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {
    }

    @Override
    public void onError(Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        showToast(info.getMsg());
    }

    @Override
    public void getNetDate() {
        initDate();
    }
}
